package com.g4.shoppingsingle.controller;


import com.g4.shoppingsingle.common.Result;
import com.g4.shoppingsingle.pojo.DdAddressInfo;
import com.g4.shoppingsingle.service.IDdAddressInfoService;
import com.g4.shoppingsingle.service.IDdUsersInfoService;
import com.g4.shoppingsingle.vo.AddressVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author admin
 * @since 2022-02-19
 */
@CrossOrigin
@Slf4j
@RestController
//@RequestMapping("/shopping/ddAddressInfo")
public class DdAddressInfoController {
    @Autowired
    private IDdAddressInfoService iDdAddressInfoService;

    /**
     * 查询地址
     *
     * @param id
     * @return
     */
    @GetMapping("/selectAddressById/{id}")
    public Result selectAddressById(@PathVariable("id") int id) {
        DdAddressInfo ddAddressInfo = iDdAddressInfoService.getById(id);
        log.info("地址信息:{}", ddAddressInfo);
        return new Result(true, "操作成功", ddAddressInfo);
    }

    /**
     * 查询地址
     *
     * @return
     */
    @GetMapping("/selectAddress")
    public Result selectAddress() {

        List<DdAddressInfo> infos = iDdAddressInfoService.selectById();
        log.info("地址信息:{}", infos);

        return new Result(true, "操作成功", infos);
    }

    /**
     * 删除 地址
     *
     * @param id
     * @return
     */


    @PostMapping("/deleteAddress/{addressId}")
    public Result deletedAddress(@PathVariable("addressId") int id) {

        iDdAddressInfoService.deleteAddress(id);
        return new Result(true, "操作成功");

    }

    /**
     * 修改地址
     */

    @PostMapping("/updateAddress")
    public Result updateAll(@RequestBody AddressVo params

    ) {

        iDdAddressInfoService.updateAddress(params.getId(), params.getStr(), params.getUserId());


        return new Result(true, "操作成功");

    }

    @PostMapping("/addAddress/{address}/{userId}")
    public Result addAddress(@PathVariable("userId") int userId,
                             @PathVariable("address") String address) {

        iDdAddressInfoService.addAddressInfo(address, userId);


        return new Result();
    }


}
